<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>刷棒棒糖</title>
<link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<style>
.container {
  margin-top: 20px;
}
.log {
  max-height: 200px;
  overflow-y: scroll;
}
</style>
</head>
<body>
  <div class="container">
    <div class="left">
      <div class="panel panel-default">
        <div class="panel-heading">新增</div>
        <div id="user_info" class="panel-body">
          <form>
            <div class="form-group">
              <label for="order_no">订单编号</label>
              <input type="text" class="form-control" id="order_no" placeholder="" value="">
            </div>
            <div class="form-group">
              <label for="username">用户名</label>
              <input type="text" class="form-control" id="username" placeholder="" value="">
            </div>
            <div class="form-group">
              <label for="share_link">分享链接</label>
              <input type="text" class="form-control" id="share_link" placeholder="" value="">
            </div>
            <div class="form-group">
              <label for="start_date">开始日期</label>
              <input type="text" class="form-control" id="start_date" data-date-format="yyyy-mm-dd" placeholder="" value="">
            </div>
            <div class="form-group">
              <label for="end_date">结束日期</label>
              <input type="text" class="form-control" id="end_date" placeholder="" value="">
            </div>
            <button type="button" class="btn btn-info" onclick="addsave()">新增</button>
            <button type="button" class="btn btn-info" onclick="back()">关闭</button>
          </form>
        </div>
      </div>
    </div>
  </div>
  <script>
  // 重命名 Electron 提供的 require
  window.nodeRequire = require;
  delete window.require;
  delete window.exports;
  delete window.module;
  </script>
  <script type="text/javascript" src="node_modules/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="node_modules/lowdb/dist/lowdb.min.js"></script>
  <script type="text/javascript" src="node_modules/lodash/lodash.js"></script>
  <script type="text/javascript" src="node_modules/dot/doT.min.js"></script>
  <script type="text/javascript" src="js/bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
  <script type="text/javascript" src="js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
  <script>
  const {BrowserWindow} = nodeRequire('electron').remote;
  let win;

  let low = nodeRequire('lowdb');
  const db = low('db.json');
  //查询order_no最大值
  let max_order_no;
  let max_user_info = db.get('user_info').orderBy('order_no', 'desc').take(1).value();
  if(max_user_info.length > 0) {
    max_order_no = max_user_info[0].order_no;
  } else {
    max_order_no = '00000'
  }

  console.log(max_order_no);
  $('#order_no').val(pad(parseInt(max_order_no)+1, 5));

  $('#start_date, #end_date').datetimepicker({
    format: 'yyyy-mm-dd',
    language: 'zh-CN',
    minView: 2,
    autoclose: true
  });

  function pad(num, n) {
    var len = num.toString().length;
    while(len < n) {
        num = "0" + num;
        len++;
    }
    return num;
  }
  function back() {
    console.log('/.................');
    nodeRequire('electron').remote.getCurrentWindow().close();
  }

  function addsave() {
    let order_no = $('#order_no').val();
    let username = $('#username').val();
    let share_link = $('#share_link').val();
    let p = getOriginUrl(share_link);
    let start_date = $('#start_date').val();
    let end_date = $('#end_date').val();
    p.then(function(result) {
      let user_url = result.user_url;
      let user_id = result.user_id;
      db.get('user_info').push({order_no: order_no, username: username, share_link: share_link, user_url: user_url, user_id: user_id, status: '', status_time: '', start_date: start_date, end_date: end_date}).value();
      //新增成功后，关闭当前页，并刷新父页面
      let ipcRenderer = nodeRequire('electron').ipcRenderer;
      ipcRenderer.send('refresh_user_list','');
      nodeRequire('electron').remote.getCurrentWindow().close();
    });
    //修改成功后，关闭当前页，并刷新父页面
    // let ipcRenderer = nodeRequire('electron').ipcRenderer;
    // ipcRenderer.send('refresh_user_list','');
    // nodeRequire('electron').remote.getCurrentWindow().close();
  }

  function getOriginUrl(url) {
    return new Promise(function(resolve, reject) {
      $.post('http://dwz.cn/query.php', {tinyurl: url}, function(result) {
        let long_url = JSON.parse(result).longurl;
        let user_id = getParam(long_url).id;
        let user_url = 'http://cn.battleofballs.com/share?type=1&id=' + user_id;
        resolve({user_url: user_url, user_id: user_id});
      });
    });
  }

  function getParam(url) {
    let result = {};
    var num=url.indexOf("?")
    let str=url.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]

    var arr=str.split("&"); //各个参数放到数组里
    for(var i=0;i < arr.length;i++){
      num=arr[i].indexOf("=");
      if(num>0){
        name=arr[i].substring(0,num);
        value=arr[i].substr(num+1);
        result[name]=value;
      }
    }
    return result;
  }
  </script>
</body>

</html>
